﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Directory.Web
{
	/// <summary>
	///DBQueryDataProxy 的摘要说明
	/// </summary>
	public class DBQueryDataProxy
	{
		public static void BackUpDB(string name)
		{
			if (Helper.ValidateStringParameter(ref name))
			{
				string diskPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DBbakDiskPath"]) + name;
				string dbName = ConfigurationManager.AppSettings["DBName"];
				string query = string.Format("BACKUP DATABASE [{2}] TO  DISK = N'{0}' WITH NOFORMAT, NOINIT,  NAME = N'{1}', SKIP, NOREWIND, NOUNLOAD,  STATS = 10", diskPath, dbName + "-Full Database Backup", dbName);
				Directory.BLL.DBQuery bll = new Directory.BLL.DBQuery();
				bll.ExecuteNonQuery(query);
			}
			else
			{
				throw new ArgumentException("backup file name invalid.");
			}
		}

		public static void RestoreDB(string name)
		{
			if (Helper.ValidateStringParameter(ref name))
			{
				string diskPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DBbakDiskPath"]) + name;
				string dbName = ConfigurationManager.AppSettings["DBName"];
				string query = string.Format("RESTORE DATABASE [{1}] FROM  DISK = N'{0}' WITH  FILE = 11,  NOUNLOAD,  STATS = 10", diskPath, dbName);
				Directory.BLL.DBQuery bll = new Directory.BLL.DBQuery();
				bll.ExecuteNonQuery(query);
			}
			else
			{
				throw new ArgumentException("backup file name invalid.");
			}
		}

		public static int ExecuteNonQuery(string query)
		{
			Directory.BLL.DBQuery bll = new Directory.BLL.DBQuery();
			return bll.ExecuteNonQuery(query);
		}

		public static object ExecuteScalar(string query)
		{
			Directory.BLL.DBQuery bll = new Directory.BLL.DBQuery();
			return bll.ExecuteScalar(query);
		}

		public static DataSet ExecuteReader(string query)
		{
			Directory.BLL.DBQuery bll = new Directory.BLL.DBQuery();
			return bll.ExecuteReader(query);
		}
	}
}
